package com.party.core.service.activity.biz;

import com.alibaba.fastjson.JSONObject;
import com.party.common.utils.StringUtils;
import com.party.core.model.activity.CrowfundResources;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.user.User;
import com.party.core.service.activity.ICrowfundResourcesService;
import com.party.core.service.user.IUserService;
import com.party.core.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CrowfundResourceBizService {
    @Autowired
    private ICrowfundResourcesService crowfundResourcesService;
    @Autowired
    private IUserService userService;

    /**
     * 保存资源业务
     *
     * @param refId     引用id
     * @param picList   图片
     * @param videoList 视频
     */
    @SuppressWarnings("unchecked")
    public void saveResourceBiz(String refId, String picList, String videoList, String memberId) throws Exception {
        User user = userService.findByLoginName("admin");
        // 跑马灯
        // 先删除原来的
        List<CrowfundResources> oldPicList = crowfundResourcesService.findByRefId(refId, "1");
        oldPicList.forEach(resource -> {
            crowfundResourcesService.deleteByUnique(resource);
        });
        // 再添加新的图片, 保证排序
        if (StringUtils.isNotEmpty(picList)) {
            List<Map<String, String>> newPicList = (List<Map<String, String>>) JSONObject.parse(picList);
            int sort = 1;
            for (Map<String, String> map : newPicList) {
                /*CrowfundResources resources = crowfundResourcesService.getUnique(new CrowfundResources(map.get("pic"), refId, "1"));
                if (resources == null) {
                    saveResource(user.getId(), refId, "1",  sort, map.get("pic"), map.get("remarks"), memberId);
                    sort++;
                }*/
                saveResource(user.getId(), refId, "1",  sort, map.get("pic"), map.get("remarks"), memberId);
                sort++;
            }
        }

        /*List<CrowfundResources> oldPicList = crowfundResourcesService.findByRefId(refId, "1");
        List<String> oldPics = LangUtils.transform(oldPicList, new Function<CrowfundResources, String>() {
            @Override
            public String apply(CrowfundResources input) {
                return input.getResourceUrl();
            }
        });

        if (StringUtils.isNotEmpty(picList)) {
            List<Map<String, String>> newPicList = (List<Map<String, String>>) JSONObject.parse(picList);
            List<String> newPics = LangUtils.transform(newPicList, new Function<Map<String, String>, String>() {
                @Override
                public String apply(Map<String, String> input) {
                    return input.get("pic");
                }
            });
            oldPics.removeAll(newPics);
        }

        for (String oldPic : oldPics) {
            crowfundResourcesService.deleteByUnique(new CrowfundResources(oldPic, refId, "1"));
        }*/

        // 视频
        String videoId = null;
        if (StringUtils.isNotEmpty(videoList)) {
            Map<String, String> map = (Map<String, String>) JSONObject.parse(videoList);
            String videoUrl = map.get("pic").replaceAll("\"", "'");
            if (StringUtils.isNotEmpty(videoUrl)) {
                CrowfundResources resources = crowfundResourcesService.getUnique(new CrowfundResources(videoUrl, refId, "2"));
                if (resources == null) {
                    videoId = saveResource(user.getId(), refId, "2", 1, videoUrl, map.get("remarks"), memberId);
                } else {
                    resources.setResourceUrl(videoUrl);
                    resources.setRemarks(map.get("remarks"));
                    crowfundResourcesService.update(resources);
                    videoId = resources.getId();
                }
            }
        }

        List<CrowfundResources> oldVideoList = crowfundResourcesService.findByRefId(refId, "2");
        if (StringUtils.isEmpty(videoId)) {
            for (CrowfundResources resources : oldVideoList) {
                crowfundResourcesService.delete(resources.getId());
            }
        } else {
            for (CrowfundResources resources : oldVideoList) {
                if (!resources.getId().equals(videoId)) {
                    crowfundResourcesService.delete(resources.getId());
                }
            }
        }
    }

    /**
     * 保存资源业务
     *
     * @param activityId  活动id
     * @param picList   图片
     * @param videoList 视频
     * @param memberId  用户id
     */
    public void saveResourceBiz(String activityId, List<CrowfundResources> picList, List<CrowfundResources> videoList, String memberId) {
        User user = userService.findByLoginName("admin");

        int sort = 1;
        for (CrowfundResources map : picList) {
            saveResource(user.getId(), activityId, "1", sort, map.getResourceUrl(), map.getRemarks(), memberId);
            sort++;
        }

        if (videoList.size() > 0) {
            CrowfundResources map = videoList.get(0);
            saveResource(user.getId(), activityId, "2", 1, map.getResourceUrl(), map.getRemarks(), memberId);
        }
    }

    /**
     * 保存资源
     *
     * @param userId
     * @param businessId
     * @param type
     * @param sort
     * @param picUrl
     * @param remarks
     */
    public String saveResource(String userId, String businessId, String type, Integer sort, String picUrl, String remarks, String memberId) {
        CrowfundResources crowfundResource = new CrowfundResources();
        crowfundResource.setCreateBy(userId);
        crowfundResource.setUpdateBy(userId);
        crowfundResource.setMember(memberId);
        crowfundResource.setRefId(businessId);
        crowfundResource.setResourceUrl(picUrl);
        crowfundResource.setRemarks(remarks);
        crowfundResource.setType(type);
        crowfundResource.setSort(sort);
        return crowfundResourcesService.insert(crowfundResource);
    }

    public void initResource(List<CrowfundResources> picList, CrowdfundEvent crowdfundEvent) throws Exception {
        for (CrowfundResources resources : picList) {
            CrowfundResources pic = new CrowfundResources();
            MyBeanUtils.copyBeanNotNull2Bean(resources, pic);
            pic.setId(null);
            pic.setCreateDate(null);
            pic.setUpdateDate(null);
            pic.setMember(crowdfundEvent.getCreateBy());
            pic.setRefId(crowdfundEvent.getId());
            crowfundResourcesService.insert(pic);
        }
    }

    public void deleteBiz(String refId) {
        crowfundResourcesService.deleteByRefId(refId, "1");
        crowfundResourcesService.deleteByRefId(refId, "2");
    }
}
